package qf.index;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.db.Db;
import cn.hutool.db.Entity;

import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;

public class ToText {

    /**
     * WOS格式的数据，转纯文本测试
     */
    public static void main(String[] args) throws SQLException {

        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        System.out.println(format.format(new Date()) + " 程序开始了~~~");

        String wosCsvTable = "scholar_25_01.spider_cn_core_csv_500";
        String utArticleTable = "scholar_25_01.spider_tb_true_92007022_1125";

//        readTable(wosCsvTable, "pid");

        List<String> list = FileUtil.readLines("dict/test.txt", "utf-8");
        for (String s : list) {
            System.out.println(s);
        }


    }

    public static void readTable(String wosCsvTable, String autoId) throws SQLException {
        int onePageNum = 1000;
        List<Entity> tableNum = Db.use().query("select count(1) as count from " + wosCsvTable);
        String filePath = "C:\\临时文件\\2024_12\\wos2.txt";
        FileUtil.appendUtf8String("FN Clarivate Analytics Web of Science" + System.lineSeparator(), filePath);
        FileUtil.appendUtf8String("VR 1.0" + System.lineSeparator(), filePath);
//        String[] split = "PT,AU,BA,BE,GP,AF,BF,CA,TI,SO,SE,BS,LA,DT,CT,CY,CL,SP,HO,DE,ID,AB,C1,RP,EM,RI,OI,FU,FX,CR,C3,WE,NR,TC,Z9,U1,U2,PU,PI,PA,SN,EI,BN,J9,JI,PD,PY,VL,IS,PN,SU,SI,MA,BP,EP,AR,DI,DL,D2,EA,EY,PG,WC,SC,GA,UT,PM,FP,OA,DA".split(",");
        String[] split = "PT,AU,TI,SO,LA,DT,DE,ID,AB,C1,RP,FU,TC,DI,WC,PY,CR".split(",");
        if (tableNum.get(0).getInt("count") > 0) {
            int tableMax = Db.use().queryNumber("select max(" + autoId + ") from " + wosCsvTable).intValue();
            Collection<Integer> tableList = new ArrayList<>();
            NumberUtil.appendRange(0, tableMax + onePageNum, onePageNum, tableList);
            for (Integer startId : tableList) {
                List<Entity> tableData = Db.use().query("select * from " + wosCsvTable + " where " + autoId + " between " + startId + " and " + (startId + onePageNum - 1));
                for (Entity entry : tableData) {
                    for (String s : split) {
                        saveField(filePath, entry.getStr(s), s);
                    }
                    FileUtil.appendUtf8String("ER" + System.lineSeparator(), filePath);
                    FileUtil.appendUtf8String(System.lineSeparator(), filePath);
                }
            }
        }
        FileUtil.appendUtf8String("EF", filePath);
    }

    public static void saveField(String filePath, String str, String field) {
        if (null != str && !str.isEmpty()) {
            FileUtil.appendUtf8String(field + " " + str.trim() + System.lineSeparator(), filePath);
        }
    }


}
